package aom.ju.ss.view.base

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import aom.ju.ss.R
import aom.ju.ss.databinding.ListActivityBinding
import aom.ju.ss.lpInterface.BindViewInterface
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.OKHttpUtils
import java.util.*

abstract class BaseListActivity : BaseActivity() {
    protected lateinit var binding: ListActivityBinding
    protected var size = 10
    protected var page = 1
    protected lateinit var pAdapter: PAdapter
    protected var post = true
    protected var ishttp = true

    override val lay: Int
        get() = R.layout.list_activity

    private var list: MutableList<Any> = ArrayList()

    fun getLists(): MutableList<Any> {
        return list
    }

    fun setLists(lists: List<Any>?) {
        if (lists != null) {
            list.addAll(lists)
        }

        binding.list.small.Fin()
        dataSize = list.size
        binding.list.noData.VG(list.size == 0)
        pAdapter.notifyDataSetChanged()
    }

    override fun init() {
        binding = viewBinding as ListActivityBinding
        binding.title.title.text = Title()
        binding.list.mRecycler.layoutManager =
            if (LayoutManager() == null) LinearLayoutManager(activity) else LayoutManager()
        pAdapter = PAdapter(list, item(), interFace())
        binding.list.mRecycler.adapter = pAdapter

        if (NoDataMSg() != "") {
            binding.list.msg.text = NoDataMSg()
        }
        if (NoDataImg() != 0) {
            binding.list.img.setImageResource(NoDataImg())
        }

        binding.list.small.setOnRefreshListener {
            page = 1
            data
        }
        binding.list.small.setOnLoadMoreListener {
            LogUtils.e("dataSize++$dataSize")
            if (dataSize != 0) {
                page++
                data
            } else {
                "没有更多数据".show
                it.finishLoadMore()
            }
        }
        if (ishttp) {
            data
        }

        ListonCreate()
    }


    protected open fun gosnView() {
        binding.list.img.visibility = View.INVISIBLE
        binding.list.msg.visibility = View.INVISIBLE
    }

    protected abstract fun interFace(): BindViewInterface
    protected abstract fun item(): Int
    protected var dataSize = 0
    protected val data: Unit
        get() {
            val ok = OKHttpUtils(this@BaseListActivity)
                .SetApiUrl(api())
                .SetKey(*key())
                .SetValue(*value())
            if (post) {
                "post".log
                ok.POST(this)
            } else {
                "get".log
                ok.GET(this)
            }

        }

    protected abstract fun ListonCreate()
    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        super.ok(jsonString, httpTY)
        binding.list.small.finishRefresh()
        binding.list.small.finishLoadMore()
        if (page == 1) {
            list.clear()
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        binding.list.small.finishRefresh()
        binding.list.small.finishLoadMore()
        e?.show()
    }

    protected open fun value(): Array<String> {
        return arrayOf()
    }

    protected open fun key(): Array<String> {
        return arrayOf()
    }

    protected open fun api(): String {
        return ""
    }

    protected open fun NoDataImg(): Int {
        return 0
    }

    protected open fun NoDataMSg(): String {
        return ""
    }

    protected open fun Title(): String {
        return ""
    }

    protected open fun LayoutManager(): RecyclerView.LayoutManager? {
        return LinearLayoutManager(activity)
    }
}